package com.liwei.lwmail.order.interceptor;

import com.liwei.common.constant.AuthServerConstant;
import com.liwei.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        boolean match = new AntPathMatcher().match("/order/order/status/**", request.getRequestURI());
        if (match){
            return true;
        }
        MemberResponseVo loginUser = (MemberResponseVo) request.getSession().getAttribute(AuthServerConstant.LOGING_USER);
        if (loginUser != null){
            threadLocal.set(loginUser);
            return true;
        }else {
            response.sendRedirect("http://auth.lwmall.com/login.html?originUrl=http://order.lwmall.com/confirm.html");
            return false;
        }
    }
}
